//========encoding:utf-8=========
/*
 * @describe:  三目运算符
 * @author:mhao
 * @copyright:chinasoft.cn
 * @date:2020年3月23日   下午9:21:11
 */
package edu.chinasoft.handler;

import java.io.File;
import java.io.IOException;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
@Controller()
@RequestMapping("/fileUploadHandler")
public class FileUploadHandler {
	//MultipartFile该类型springmvc对文件上传组件的再一次封装
	@RequestMapping(value="/fileupload.do",method=RequestMethod.POST)
	public ModelAndView method01(MultipartFile fileupload,HttpServletRequest request,ModelAndView modelAndView) throws Exception{
		//获取所要上传文件的路径
		String dirFile = request.getRealPath("/upload");
		//如果父亲目录不在，则手动创建该目录
		File fatherDir = new File(dirFile);
		if(!fatherDir.exists()) {
			fatherDir.mkdirs();
		}
		//获取所要上传文件的名字
		String fileName = fileupload.getOriginalFilename();
		//实现文件上传
		try {
			fileupload.transferTo(new File(dirFile,fileName));
		} catch (Exception e) {
			// TODO Auto-generated catch block
			throw e;
		}
		modelAndView.addObject("result","文件:"+fileName+"上传成功。");
		modelAndView.setViewName("/jsp02_result");
		return modelAndView;
	}
	@RequestMapping(value = "/fileupload01.do", method = RequestMethod.POST)
    public ModelAndView method02( @RequestParam(value = "fileupload") MultipartFile[] fileuploadArray, HttpServletRequest request, ModelAndView modelAndView) throws Exception {
        System.out.println(fileuploadArray.length);
        //获取所要上传文件的路径
        String dirFile = request.getRealPath("/upload");
        //如果父亲目录不在，则手动创建该目录
        File fatherFile = new File(dirFile);
        if(!fatherFile.exists()) {
        	fatherFile.mkdirs();
        }
        for(MultipartFile fileupload:fileuploadArray) {
        	new Thread(new FileUploadThread(fileupload,dirFile)).start();
        }
        modelAndView.setViewName("jsp02_result");
		return modelAndView;
	}
	//实现文件上传
    class  FileUploadThread implements Runnable{
        private MultipartFile fileupload;
        private String fatherDir;

        public FileUploadThread() {
        }

        public FileUploadThread(MultipartFile fileupload, String fatherDir) {
            this.fileupload = fileupload;
            this.fatherDir = fatherDir;
        }

		@Override
		public void run() {
			// TODO Auto-generated method stub
			//获取所要上传文件的名字
			String filename = fileupload.getOriginalFilename();
			//实现文件上传
			try {
				fileupload.transferTo(new File(fatherDir,filename));
			}catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
        
    }
        
}
